﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using OgilvyOne.IDAL;
using OgilvyOne.SqlServerDAL;
using OgilvyOne.Model;
namespace OgilvyOne.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:BrandInfoDal
    /// </summary>
    public partial class BrandInfoDal : IBrandInfo
    {
        string connstr = SQLHelper.CONN_STRING_NON_DTC;
        public BrandInfoDal()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string name,int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from COFCO_BRAND_INFO");
            strSql.Append(" where BRAND_NAME=@BRAND_NAME and BRAND_ID not in ("+id+")");
            SqlParameter[] parameters = {
					new SqlParameter("@BRAND_NAME", SqlDbType.VarChar,100)
                    };
            parameters[0].Value = name;

            return int.Parse(SQLHelper.ExecuteScalar(connstr,CommandType.Text,strSql.ToString(),parameters).ToString())>0;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(BrandInfoModel model,string typeIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"declare @num int;select @num=max(ORDER_NUM)from COFCO_BRAND_INFO where BRAND_TYPE_ID in ("+typeIdlist+");");
            strSql.Append("insert into COFCO_BRAND_INFO(");
            strSql.Append("BRAND_NAME,BRAND_IMG,BRAND_DESC,BRAND_TYPE_ID,ORDER_NUM,STATE,ADD_TIME,ADDUSERNAME)");
            strSql.Append(" values (");
            strSql.Append("@BRAND_NAME,@BRAND_IMG,@BRAND_DESC,@BRAND_TYPE_ID,@num+1,@STATE,@ADD_TIME,@ADDUSERNAME)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@BRAND_NAME", SqlDbType.VarChar,100),
					new SqlParameter("@BRAND_IMG", SqlDbType.VarChar,50),
					new SqlParameter("@BRAND_DESC", SqlDbType.VarChar,800),
					new SqlParameter("@BRAND_TYPE_ID", SqlDbType.Int,4),					
					new SqlParameter("@STATE", SqlDbType.Int,4),
					new SqlParameter("@ADD_TIME", SqlDbType.DateTime),
					new SqlParameter("@ADDUSERNAME", SqlDbType.VarChar,50)};
            parameters[0].Value = model.Brandname;
            parameters[1].Value = model.Brandimg;
            parameters[2].Value = model.Branddesc;
            parameters[3].Value = model.Brandtypeid;           
            parameters[4].Value = model.State;
            parameters[5].Value = model.Addtime;
            parameters[6].Value = model.Addusername;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters);
            
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(BrandInfoModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update COFCO_BRAND_INFO set ");
            strSql.Append("BRAND_NAME=@BRAND_NAME,");
            strSql.Append("BRAND_IMG=@BRAND_IMG,");
            strSql.Append("BRAND_DESC=@BRAND_DESC,");
            strSql.Append("BRAND_TYPE_ID=@BRAND_TYPE_ID,");
            strSql.Append("ORDER_NUM=@ORDER_NUM,");
            strSql.Append("STATE=@STATE,");
            strSql.Append("ADD_TIME=@ADD_TIME,");
            strSql.Append("ADDUSERNAME=@ADDUSERNAME");
            strSql.Append(" where BRAND_ID=@BRAND_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@BRAND_NAME", SqlDbType.VarChar,100),
					new SqlParameter("@BRAND_IMG", SqlDbType.VarChar,50),
					new SqlParameter("@BRAND_DESC", SqlDbType.VarChar,800),
					new SqlParameter("@BRAND_TYPE_ID", SqlDbType.Int,4),
					new SqlParameter("@ORDER_NUM", SqlDbType.Int,4),
					new SqlParameter("@STATE", SqlDbType.Int,4),
					new SqlParameter("@ADD_TIME", SqlDbType.DateTime),
					new SqlParameter("@ADDUSERNAME", SqlDbType.VarChar,50),
					new SqlParameter("@BRAND_ID", SqlDbType.Int,4)};
            parameters[0].Value = model.Brandname;
            parameters[1].Value = model.Brandimg;
            parameters[2].Value = model.Branddesc;
            parameters[3].Value = model.Brandtypeid;
            parameters[4].Value = model.Ordernum;
            parameters[5].Value = model.State;
            if (model.Addtime == null)
            {
                parameters[6].Value = DBNull.Value;
            }
            else
                parameters[6].Value = model.Addtime;
            parameters[7].Value = model.Addusername;
            parameters[8].Value = model.Brandid;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters)>0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int BRAND_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_BRAND_INFO ");
            strSql.Append(" where BRAND_ID=@BRAND_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@BRAND_ID", SqlDbType.Int,4)
};
            parameters[0].Value = BRAND_ID;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters)>0;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string BRAND_IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_BRAND_INFO ");
            strSql.Append(" where BRAND_ID in (" + BRAND_IDlist + ")  ");
            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), null)>0;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BrandInfoModel GetModel(int BRAND_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 BRAND_ID,BRAND_NAME,BRAND_IMG,BRAND_DESC,BRAND_TYPE_ID,ORDER_NUM,STATE,ADD_TIME,ADDUSERNAME from COFCO_BRAND_INFO ");
            strSql.Append(" where BRAND_ID="+BRAND_ID);          
            BrandInfoModel model = new BrandInfoModel();
            DataSet ds = SQLHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["BRAND_ID"].ToString() != "")
                {
                    model.Brandid = int.Parse(ds.Tables[0].Rows[0]["BRAND_ID"].ToString());
                }
                model.Brandname = ds.Tables[0].Rows[0]["BRAND_NAME"].ToString();
                model.Brandimg= ds.Tables[0].Rows[0]["BRAND_IMG"].ToString();
                model.Branddesc = ds.Tables[0].Rows[0]["BRAND_DESC"].ToString();
                if (ds.Tables[0].Rows[0]["BRAND_TYPE_ID"].ToString() != "")
                {
                    model.Brandtypeid = int.Parse(ds.Tables[0].Rows[0]["BRAND_TYPE_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ORDER_NUM"].ToString() != "")
                {
                    model.Ordernum = int.Parse(ds.Tables[0].Rows[0]["ORDER_NUM"].ToString());
                }
                if (ds.Tables[0].Rows[0]["STATE"].ToString() != "")
                {
                    model.State  = int.Parse(ds.Tables[0].Rows[0]["STATE"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ADD_TIME"].ToString() != "")
                {
                    model.Addtime = DateTime.Parse(ds.Tables[0].Rows[0]["ADD_TIME"].ToString());
                }
                model.Addusername = ds.Tables[0].Rows[0]["ADDUSERNAME"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BrandInfoModel GetModelByOrderNum(int orderNum,string idlist)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 BRAND_ID,BRAND_NAME,BRAND_IMG,BRAND_DESC,BRAND_TYPE_ID,ORDER_NUM,STATE,ADD_TIME,ADDUSERNAME from COFCO_BRAND_INFO ");
            strSql.Append(" where ORDER_NUM=" + orderNum + "and BRAND_TYPE_ID in ("+idlist+")");
            BrandInfoModel model = new BrandInfoModel();
            DataSet ds = SQLHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["BRAND_ID"].ToString() != "")
                {
                    model.Brandid = int.Parse(ds.Tables[0].Rows[0]["BRAND_ID"].ToString());
                }
                model.Brandname = ds.Tables[0].Rows[0]["BRAND_NAME"].ToString();
                model.Brandimg = ds.Tables[0].Rows[0]["BRAND_IMG"].ToString();
                model.Branddesc = ds.Tables[0].Rows[0]["BRAND_DESC"].ToString();
                if (ds.Tables[0].Rows[0]["BRAND_TYPE_ID"].ToString() != "")
                {
                    model.Brandtypeid = int.Parse(ds.Tables[0].Rows[0]["BRAND_TYPE_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ORDER_NUM"].ToString() != "")
                {
                    model.Ordernum = int.Parse(ds.Tables[0].Rows[0]["ORDER_NUM"].ToString());
                }
                if (ds.Tables[0].Rows[0]["STATE"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["STATE"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ADD_TIME"].ToString() != "")
                {
                    model.Addtime = DateTime.Parse(ds.Tables[0].Rows[0]["ADD_TIME"].ToString());
                }
                model.Addusername = ds.Tables[0].Rows[0]["ADDUSERNAME"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BRAND_ID,BRAND_NAME,BRAND_IMG,BRAND_DESC,BRAND_TYPE_ID,ORDER_NUM,STATE,ADD_TIME,ADDUSERNAME ");
            strSql.Append(" FROM COFCO_BRAND_INFO ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by ORDER_NUM");
            return SQLHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" BRAND_ID,BRAND_NAME,BRAND_IMG,BRAND_DESC,BRAND_TYPE_ID,ORDER_NUM,STATE,ADD_TIME,ADDUSERNAME ");
            strSql.Append(" FROM COFCO_BRAND_INFO ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "COFCO_BRAND_INFO";
            parameters[1].Value = "BRAND_ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

